define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/clues/record/index',
                    del_url: 'facrm/clues/record/del'
                }
            });

            //联系人
            var contact  = $("#table");
            //在普通搜索渲染后
            contact.on('post-common-search.bs.table', function (event, table) {
 
            });
            contact.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar',
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    //params.filter='{"customer_id":'+customer_id+'}';//只是查当前客户的联系人
                    return params
                },
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {
                            field: 'types_id',
                            title: __('线索'),
                            align: 'left',
                            formatter: function (value, row, index) {
                                if (!row.clues) return '-';
                                value='<a href="javascript:void(0);" data-url="facrm/clues/index/edit/ids/' + row.types_id
                                    +'" data-area="[&quot;100%&quot;,&quot;100%&quot;]" class="btn-dialog" data-title="'+ row.clues.name+'">'+ row.clues.name+'</a>';
                                return value;
                            },  addclass: 'selectpage',
                            extend: 'data-source="facrm/clues/index" data-field="name"'
                        },

                        {field: 'content', title: __('content'), operate: 'LIKE',align: 'left'},
                        {field: 'create_user_id', title: __('创建人'),formatter: function (value, row, index) {
                                if (!row.create_user) return '-';
                                return row.create_user.nickname;
                            },    addclass: 'selectpage',
                            extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname"'},

                        {field: 'create_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
						extend: 'autocomplete="off"'},
                        {field: 'next_time', title: __('下次跟进'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
						extend: 'autocomplete="off"'},
                        {field: 'record_type', title: __('跟进类型'),formatter: function (v,r) {
                                return record_type_list[v];
                            },searchList: record_type_list},
                        {field: 'operate', title: __('Operate'), table: contact, align: 'left',
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'files',
                                    text: '附件',
                                    title: '查看附件',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url:function (row) {
                                        return  'facrm/clues/record/files/types/'+row.types+'/ids/'+row.id;
                                    },
                                    hidden:function (row) {
                                        if (row.files.length<=0) return true;
                                    }

                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ],
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });
            // 初始化表格
            contact.bootstrapTable();
            Table.api.bindevent(contact);//当内容渲染完成后
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name="+field+"]").val(value);
                return false;
            });




        },
        add: function () {
            Controller.api.bindevent();
            if ("undefined" == typeof clues_id){
                clues_id=0;
            }
            Fast.api.ajax({
                url: 'facrm/fields/get_fields_html',
                data: {source: 'clues', 'id': clues_id,'tpl':"fields_6_2"}
            }, function (data) {
                $("#extend").html(data.html);
                Form.api.bindevent($("#extend"));
                return false;
            });
            $("#buttonshow").click(function () {
                var hiddeninfo= $("#hiddeninfo");
                hiddeninfo.toggle();
                if(hiddeninfo.css('display') === 'none'){
                    $(this).text("显示更多");
                }else{
                    $(this).text("隐藏信息");
                }
            });

            // 初始化表格参数配置

            Table.api.init({
                extend: {
                    index_url: 'facrm/clues/record/index/clues_id/'+clues_id,
                    files_url:'facrm/clues/record/files/clues_id/'+clues_id,
                }
            });
            var table = $("#tableflow");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar1',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'content', title: __('content'), operate: 'LIKE'},
                        {field: 'create_user.nickname', title: __('创建人'),operate:false},
                        {field: 'create_user_id', title: __('创建人'), visible:false,addclass: 'selectpage',
                            extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname"'},

                        {field: 'create_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
						extend: 'autocomplete="off"'},
                        {field: 'next_time', title: __('下次跟进'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
						extend: 'autocomplete="off"'},

                        {field: 'files', title: __('附件'),formatter: function (v,row) {
                                if (v.length>0) return '<a href="facrm/clues/record/files/types/'+row.types+'/ids/'+row.id+'" class="btn btn-success btn-dialog btn-xs"><i class="fa fa-file-image-o"></i> ' + __('附件') + '</a>';
                            },operate: false},
                        {field: 'record_type', title: __('跟进类型'),formatter: function (v,r) {
                                return record_type_list[v];
                            },searchList: record_type_list},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);




            //附件
            var files  = $("#t-files");
            files.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.files_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-files',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 12,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: false,
                columns: [
                    [
                        {field: 'url', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'create_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,
						extend: 'autocomplete="off"'},

                    ]
                ],
            });
            //发邮件|发信息
            $(".btn-send-email,.btn-send-sms").click(function () {
                var that = this;
                var title = $(that).data('title') || $(that).attr("title") || __('操作');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title,data.url;
                parent.Fast.api.open(url+"?data="+encodeURI(JSON.stringify(data)), title, data);
            });

            //下一条线索
            $(".btn-next").click(function () {
                Fast.api.ajax({
                    url: $(this).data('url'),
                    data: {}
                }, function (data) {
                    setTimeout("closetab()",'100');
                    Backend.api.addtabs('facrm/clues/record/add/ids/'+data.id,'跟进线索-'+data.name);

                    return false;
                });
            });



        },
        edit: function () {

        },

        files: function () {

        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), function (data) {
                    $("#tableflow").bootstrapTable("refresh");
                    $("#record-content").val("");
                    return true;
                });
                //云呼
                $(".cloudcall").click(function () {
                    var that = this;
                    var type = $(that).data('type');
                    var typeid = $(that).data('typeid');
                    var name = $(that).data('name');
                    var field = $(that).data('field');

                    Fast.api.ajax({
                        url: 'facrm/setting/cloudcall/call',
                        data: {type: type, typeid: typeid,field:field,name:name}
                    }, function (data) {
                        Toastr.success("呼叫成功！");
                        return false;
                    });
                });
            }
            ,
            formatter: {
                thumb: function (value, row, index) {
                    var reg = /\.(png|jpg|gif|jpeg|webp)$/;
                    if (reg.test(row.url)) {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    } else {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                },
                filename: function (value, row, index) {
                    return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
            }
        }
    };
    return Controller;
});
function  closetab(){
    top.window.$("ul.nav-addtabs li.active").prev().find(".close-tab").trigger("click");
}